package com.system.function.service;

import com.system.Tools.TimeTools;
import com.system.function.dao.WarehouseDAO;
import com.system.function.object.Commodity;
import com.system.function.object.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WarehouseService {
    @Autowired
    private WarehouseDAO WarehouseDAO;
    @Autowired
    private TimeTools timeTools;
    @Autowired
    private CommodityService commodityService;

    //查询全部
    public List<Warehouse> getAllWarehouse(Warehouse Warehouse) {
        List<Warehouse> allWarehouse = new ArrayList<>();
        try {
            List<Warehouse> allWarehouseSearch = WarehouseDAO.getAllWarehouse(Warehouse);
            for (Warehouse warehouse : allWarehouseSearch) {
                com.system.function.object.Warehouse warehouseParam = new Warehouse();
                warehouseParam.setId(warehouse.getId());
                allWarehouse.add(getWarehouse(warehouseParam));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allWarehouse;
    }

    //查询总数
    public Integer getCountWarehouse(Warehouse Warehouse) {
        return WarehouseDAO.getCountWarehouse(Warehouse);
    }

    //查询
    public Warehouse getWarehouse(Warehouse Warehouse) {
        Warehouse WarehouseSearch = null;
        try {
            WarehouseSearch = WarehouseDAO.getWarehouse(Warehouse);
            //查询当前仓库下的剩余商品数量
            Commodity commodity = new Commodity();
            commodity.setWarehouseId(WarehouseSearch.getId());

            Integer countCommodity = commodityService.getCountCommodity(commodity);
            WarehouseSearch.setCountNum(countCommodity);


            Integer countCommoditySum = commodityService.countSUmCommodity(commodity);
            WarehouseSearch.setCountSumNum(countCommoditySum);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return WarehouseSearch;
    }

    //增加
    public String addWarehouse(Warehouse Warehouse) {
        String isSuccess = "false";
        try {
            Warehouse.setId(timeTools.getTimeInt());
            int resultNum = WarehouseDAO.addWarehouse(Warehouse);
            if (resultNum > 0) {
                isSuccess = "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }

    //删除
    public String delWarehouse(Warehouse Warehouse) {
        String isSuccess = "false";
        try {
            int resultNum = WarehouseDAO.delWarehouse(Warehouse);
            if (resultNum > 0) {
                isSuccess = "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    //修改
    public String updateWarehouse(Warehouse Warehouse) {
        String isSuccess = "false";
        try {
            int resultNum = WarehouseDAO.updateWarehouse(Warehouse);
            if (resultNum > 0) {
                isSuccess = "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }
}
